import type { RouteRecordRaw } from "vue-router"
import { createRouter } from "vue-router"
import { routerConfig } from "@/router/config"
import { registerNavigationGuard } from "@/router/guard"
import { flatMultiLevelRoutes } from "./helper"

const Layouts = () => import("@/layouts/index.vue")

/**
 * @name 常驻路由
 * @description 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置唯一的 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/pages/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/pages/error/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/pages/error/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/pages/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/register",
    component: () => import("@/pages/register/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/pages/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  },
  {
    path: "/users", // 用户管理页面的路由路径
    component: Layouts,
    name: "UserManagement", // 路由名称
    meta: {
      title: "用户管理", // 侧边栏菜单标题
      elIcon: "UserFilled" // 侧边栏菜单图标
    },
    children: [
      {
        path: "list", // 用户列表子路由
        component: () => import("@/pages/user-management/index.vue"), // 指向用户管理页面组件
        name: "UserList",
        meta: {
          title: "用户列表",
          keepAlive: true // 保持页面缓存
        }
      }
    ]
  },
  {
    path: "/statistics",
    component: Layouts,
    name: "Statistics",
    meta: {
      title: "数据统计",
      elIcon: "DataAnalysis"
    },
    children: [
      {
        path: "index",
        component: () => import("@/pages/statistics/index.vue"),
        name: "StatisticsIndex",
        meta: {
          title: "数据统计",
          keepAlive: true
        }
      }
    ]
  },
  {
    path: "/shop-management",
    component: Layouts,
    name: "ShopManagement",
    meta: {
      title: "商铺管理",
      elIcon: "Shop"
    },
    children: [
      {
        path: "index",
        component: () => import("@/pages/shop-management/index.vue"),
        name: "ShopManagementIndex",
        meta: {
          title: "商铺管理",
          keepAlive: true
        }
      }
    ]
  },
  {
    path: "/delivery-stats",
    component: Layouts,
    name: "DeliveryStats",
    meta: {
      title: "配送统计",
      elIcon: "Van"
    },
    children: [
      {
        path: "index",
        component: () => import("@/pages/delivery-stats/index.vue"),
        name: "DeliveryStatsIndex",
        meta: {
          title: "配送统计",
          keepAlive: true
        }
      }
    ]
  }
]

/**
 * @name 动态路由
 * @description 用来放置有权限 (Roles 属性) 的路由
 * @description 必须带有唯一的 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [
]

/** 路由实例 */
export const router = createRouter({
  history: routerConfig.history,
  routes: routerConfig.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  try {
    // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    location.reload()
  }
}

// 注册路由导航守卫
registerNavigationGuard(router)
